package com.d3works.apps.businesssupport.service;

import com.d3works.apps.base.db.Wizard;
import com.d3works.apps.businesssupport.db.ProjectAnnouncement;
import com.d3works.apps.businesssupport.db.repo.ProjectAnnouncementRepository;
import com.d3works.apps.hr.service.project.ProjectActivityDashboardServiceHRImpl;
import com.d3works.apps.project.db.Project;
import com.d3works.i18n.I18n;
import com.d3works.mail.db.MailMessage;
import com.d3works.meta.schema.actions.ActionView;
import com.d3works.rpc.ActionResponse;
import com.google.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ProjectActivityDashboardServiceBusinessSupportImpl
    extends ProjectActivityDashboardServiceHRImpl
    implements ProjectActivityDashboardBusinessSupportService {

  @Inject protected ProjectAnnouncementRepository projectAnnouncementRepo;

  @Override
  protected String getActionLink(String model) {
    if (ProjectAnnouncement.class.getName().equals(model)) {
      return "#/ds/project.announcement/edit/";
    }
    return super.getActionLink(model);
  }

  @Override
  protected List<String> getRelatedModels() {
    List<String> relatedModelsList = super.getRelatedModels();
    relatedModelsList.add(ProjectAnnouncement.class.getName());
    return relatedModelsList;
  }

  @Override
  protected Project getActivityProject(
      Project project, MailMessage message, Set<Long> projectIdSet) {
    if (ProjectAnnouncement.class.getName().equals(message.getRelatedModel())) {
      ProjectAnnouncement announcement = projectAnnouncementRepo.find(message.getRelatedId());
      if (announcement != null) {
        Project announcementproject = announcement.getProject();
        if (project == null
            || (announcementproject != null
                && projectIdSet.contains(announcementproject.getId()))) {
          return announcementproject;
        }
      }
    }
    return super.getActivityProject(project, message, projectIdSet);
  }

  @Override
  protected Map<String, Object> getModelWithUtilityClass(String model) {
    Map<String, Object> dataMap = super.getModelWithUtilityClass(model);
    if (ProjectAnnouncement.class.getName().equals(model)) {
      dataMap.put("modelName", "Project announcement");
      dataMap.put("utilityClass", "label-info");
    }
    return dataMap;
  }

  @Override
  public ActionResponse getProjectActivityView(Long announcementId) {
    ActionResponse response = new ActionResponse();
    response.setView(
        ActionView.define(I18n.get("Project Activity"))
            .model(Wizard.class.getName())
            .add("form", "project-activity-announcement-dashboard-form")
            .context("announcementDate", projectAnnouncementRepo.find(announcementId).getDate())
            .map());

    return response;
  }
}
